package org.luckystrophe.snowtoms.adapter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.luckystrophe.snowtoms.R;
import org.luckystrophe.snowtoms.ReplyActivity_;
import org.luckystrophe.snowtoms.manager.SnowtomsUtils;
import org.luckystrophe.snowtoms.model.JsonRequest;
import org.luckystrophe.snowtoms.model.impl.GetMessageReplies;
import org.luckystrophe.snowtoms.model.impl.Reply;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.googlecode.androidannotations.annotations.AfterInject;
import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.Background;
import com.googlecode.androidannotations.annotations.Bean;
import com.googlecode.androidannotations.annotations.EBean;
import com.googlecode.androidannotations.annotations.RootContext;
import com.googlecode.androidannotations.annotations.UiThread;

@EBean
public class AdapterReplies extends BaseAdapter {

	@RootContext
	Context context;

	@Bean(GetMessageReplies.class)
	JsonRequest getRepliesRequest;

	long msgId;

	AQuery aq;

	ReplyHolder replyHolder;

	ArrayList<Reply> arrayReplies;

	@AfterInject
	public void afterInject() {
		arrayReplies = new ArrayList<Reply>();
	}

	@AfterViews
	public void afterViews() {
		getReplies(msgId);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			replyHolder = new ReplyHolder();
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.row_reply, null);

			aq = new AQuery(convertView);

			replyHolder.iv_usrImage = aq.id(R.id.iv_replyImage).getImageView();
			replyHolder.tv_usrId = aq.id(R.id.tv_replyUsrname).getTextView();
			replyHolder.tv_replyComment = aq.id(R.id.tv_replyComment).getTextView();
			replyHolder.tv_replyDate = aq.id(R.id.tv_replyDate).getTextView();

			convertView.setTag(replyHolder);

		} else {
			replyHolder = (ReplyHolder) convertView.getTag();
		}
		aq = new AQuery(convertView);
		Reply reply = arrayReplies.get(position);
		aq.id(replyHolder.iv_usrImage).image(reply.getUsrImage());
		aq.id(replyHolder.tv_usrId).text(reply.getName());
		aq.id(replyHolder.tv_replyComment).text(reply.getReplyComment());
		aq.id(replyHolder.tv_replyDate).text(reply.getReplyDate());
		return convertView;
	}

	class ReplyHolder {
		ImageView iv_usrImage;
		TextView tv_usrId;
		TextView tv_replyComment;
		TextView tv_replyDate;
	}

	@Override
	public int getCount() {
		return arrayReplies.size();
	}

	@Override
	public Object getItem(int position) {
		return arrayReplies.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Background
	public void getReplies(long msgId) {
		((GetMessageReplies) getRepliesRequest).setMsgId(msgId);
		JSONObject resultJo = new JSONObject();
		try {
			resultJo = getRepliesRequest.sendJsonRequest();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		afterGetReplies(resultJo);
	}

	@UiThread
	public void afterGetReplies(JSONObject resultJo) {
		arrayReplies = new ArrayList<Reply>();

		try {
			JSONObject params = resultJo.getJSONObject("params");
			JSONArray replies = params.getJSONArray("replies");
			int replyCount = replies.length();

			for (int i = 0; i < replyCount; i++) {
				JSONObject reply = replies.getJSONObject(i);

				long replyId = reply.getLong("reply_id");
				long msgId = reply.getLong("msg_id");
				long usrId = reply.getLong("usr_id");

				String stringDate = reply.getString("reply_date");
				Date replyDate = SnowtomsUtils.getDate(stringDate);
				String replyComment = reply.getString("reply_comment");
				String loginType = reply.getString("login_type");
				String collaboId = reply.getString("collabo_id");
				String name = reply.getString("name");

				arrayReplies.add(new Reply(usrId, replyId, msgId, replyComment, replyDate, loginType, collaboId, name));
			}
			this.notifyDataSetChanged();
		} catch (JSONException je) {
		}
		if (getCount() == 0) {
			((ReplyActivity_) context).setNoreplyText(View.VISIBLE);
		} else {
			((ReplyActivity_) context).setNoreplyText(View.GONE);
		}

	}

	public void setMsgid(long msgId) {
		this.msgId = msgId;
	}

}
